package department.fc.controller;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import department.fc.common.AttributeAndView;
import department.fc.common.Controller;
import department.fc.domain.Department;
import department.fc.service.DepartmentService;


public class ModifyDepartmentController implements Controller{

	@Override
	public AttributeAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		String departmentId = request.getParameter("departmentId");
		String departmentName = request.getParameter("departmentName");
		String location = request.getParameter("location");
		Department department = new Department(departmentId, departmentName,
				location);
		DepartmentService service = DepartmentService.getInstance();
		try {
			service.modifyDepartment(department);
			response.sendRedirect("/DepartmentManager/GetDepartmentServlet?departmentId="
					+ departmentId);
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("error_message", e.getMessage());
			RequestDispatcher rdp = request.getRequestDispatcher("/error.jsp");
			rdp.forward(request, response);
		}
		
		return null;
	}
	
}
